/**
 * Created by DevilsEyes on 2016/4/14.
 */

var Vue = require('./lib/vue.common.min.js');
var VueHttp = require('./lib/vue-resource.min.js');
var VueRouter = require('./lib/vue-router.min.js');

var Message = require('./component/message/message.es6');
var Modal = require('./component/modal/modal.es6');

let VueComponent_SideNav = require('./component/sideNav/sideNav.vue');
let VueComponent_banner = require('./component/banner/banner.vue');

var Page_homeworkList = require('./page/homework/homeworkList.vue');
var Page_homeworkDetail = require('./page/homework/homeworkDetail.vue');

var Page_courseList = require('./page/course/courseList.vue');
var Page_courseDetail = require('./page/course/courseDetail.vue');

var Page_noticeList = require('./page/notice/noticeList.vue');

var Page_login = require('./page/login/login.vue');

var Page_verify = require('./page/verify/verify.vue');


let Login = window.Login;

//VueJs开始运作
Login.then(function (data) {
    Vue.use(VueHttp);
    Vue.use(VueRouter);
    Message.init();
    Modal.init();

    var router = new VueRouter();

    //设置登录状态
    Login.isLogin = !!data.user;
    if (Login.isLogin == false && location.hash.match('verify') == null) {
        router.replace('/login');
    }

    var App_Abstract = Vue.extend({
        data: ()=> {
            return {
                width: 0,
                height: 0,
                bannerShow: false,
                tab: '',
                isLogin: Login.isLogin
            }
        },
        methods: {
            resize: function () {
                this.width = (()=> {
                    if (window.innerWidth)
                        return window.innerWidth;
                    else if ((document.body) && (document.body.clientWidth))
                        return document.body.clientWidth;
                })();

                this.height = (()=> {
                    if (window.innerHeight)
                        return window.innerHeight;
                    else if ((document.body) && (document.body.clientHeight))
                        return document.body.clientHeight;
                })();
            }
        },
        components: {
            SideNav: VueComponent_SideNav,
            banner: VueComponent_banner
        },
        ready: function () {
            this.resize();
        }
    });

    //重定向map
    router.redirect({
        '/': '/notice'
    });

    //路由map
    router.map({
        '/login': {
            name: 'login',
            component: Page_login
        },
        '/notice': {
            name: 'notice',
            component: Page_noticeList
        },
        '/courseList': {
            name: 'courseList',
            component: Page_courseList
        },
        '/courseDetail/:id': {
            name: 'courseDetail',
            component: Page_courseDetail
        },
        '/homeworkList': {
            name: 'homeworkList',
            component: Page_homeworkList
        },
        '/homeworkDetail/:id': {
            name: 'homeworkDetail',
            component: Page_homeworkDetail
        },
        '/verify/:id': {
            name: 'verify',
            component: Page_verify
        },
        '*': {
            component: {
                template: '<span>notFound</span>'
            }
        }
    });

    router.start(App_Abstract, '#app');

    window.router = router;
});